package com.util.description;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2024/01/01 23:13
 */
public interface TypeDesc {

    static TypeDesc of(String className) {
        return new DefaultTypeDesc(className);
    }

    default boolean isWildcard() {
        return false;
    }

    default WildcardTypeDesc asWildcard() {
        if (!isWildcard()) {
            throw new IllegalStateException();
        }
        return (WildcardTypeDesc) this;
    }

    default boolean isArray() {
        return false;
    }

    default ArrayTypeDesc asArray() {
        if (!isArray()) {
            throw new IllegalStateException();
        }
        return (ArrayTypeDesc) this;
    }

    default boolean isTypeVariable() {
        return false;
    }

    default TypeVariableTypeDesc asTypeVariable() {
        if (!isTypeVariable()) {
            throw new IllegalStateException();
        }
        return (TypeVariableTypeDesc) this;
    }

    default boolean isPrimitive() {
        return false;
    }

    default PrimitiveTypeDesc asPrimitive() {
        if (!isPrimitive()) {
            throw new IllegalStateException();
        }
        return (PrimitiveTypeDesc) this;
    }

    default boolean isParameterized() {
        return false;
    }

    default ParameterizedTypeDesc asParameterized() {
        if (!isParameterized()) {
            throw new IllegalStateException();
        }
        return (ParameterizedTypeDesc) this;
    }

    String className();
}

class DefaultTypeDesc implements TypeDesc {
    String className;

    public DefaultTypeDesc(String className) {
        this.className = className;
    }

    @Override
    public String className() {
        return className;
    }
}